/*
 * Copyright (C) 2008 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package stealth.novaposhta;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.AdapterContextMenuInfo;

public class TracksList extends ListActivity {
    private static final int ACTIVITY_CREATE = 0;
    private static final int ACTIVITY_EDIT = 1;

    private static final int INSERT_ID = 1;
    private static final int EDIT_ID = 2;
    private static final int DELETE_ID = 3;
    private static final int REFRESH_ID = 4;

    private TracksDbAdapter mDbHelper;
    private Cursor mTracksCursor;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tracks_list);
        mDbHelper = new TracksDbAdapter(this);
        mDbHelper.open();
        fillData();
        registerForContextMenu(getListView());
    }

    private void fillData() {
        mTracksCursor = mDbHelper.fetchAllTracks();
        startManagingCursor(mTracksCursor);

        String[] from = new String[]{TracksDbAdapter.KEY_TITLE};

        int[] to = new int[]{R.id.text1};

        SimpleCursorAdapter tracks = 
            new SimpleCursorAdapter(this, R.layout.tracks_row, mTracksCursor, from, to);
        setListAdapter(tracks);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu.add(0, INSERT_ID, 0, R.string.menu_insert);
        menu.add(0, REFRESH_ID, 0, R.string.menu_refresh);
        return true;
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        switch(item.getItemId()) {
            case INSERT_ID:
                createNote();
                return true;
        }

        return super.onMenuItemSelected(featureId, item);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(0, DELETE_ID, 0, R.string.menu_delete);
        menu.add(0, EDIT_ID, 0, R.string.menu_edit);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
    	AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    	switch(item.getItemId()) {
        	case EDIT_ID:
        		editNote(info.id);
        		break;
        	case DELETE_ID:
                mDbHelper.deleteTrack(info.id);
                break;
        }
    	fillData();
        return super.onContextItemSelected(item);
    }

    private void createNote() {
        Intent i = new Intent(this, TrackEdit.class);
        startActivityForResult(i, ACTIVITY_CREATE);
    }
    
    private void editNote(long id) {
    	Cursor c = mDbHelper.fetchTrack(id);
    	Intent i = new Intent(this, TrackEdit.class);
        i.putExtra(TracksDbAdapter.KEY_ROWID, id);
        i.putExtra(TracksDbAdapter.KEY_TITLE, c.getString(
                c.getColumnIndexOrThrow(TracksDbAdapter.KEY_TITLE)));
        i.putExtra(TracksDbAdapter.KEY_TRACK, c.getString(
                c.getColumnIndexOrThrow(TracksDbAdapter.KEY_TRACK)));
        startActivityForResult(i, ACTIVITY_EDIT);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        editNote(id);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        if (intent == null)  return;
        Bundle extras = intent.getExtras();
        switch(requestCode) {
            case ACTIVITY_CREATE:
                String title = extras.getString(TracksDbAdapter.KEY_TITLE);
                String track = extras.getString(TracksDbAdapter.KEY_TRACK);
                mDbHelper.createTrack(title, track);
                fillData();
                break;
            case ACTIVITY_EDIT:
                Long rowId = extras.getLong(TracksDbAdapter.KEY_ROWID);
                if (rowId != null) {
                    String editTitle = extras.getString(TracksDbAdapter.KEY_TITLE);
                    String editTrack = extras.getString(TracksDbAdapter.KEY_TRACK);
                    mDbHelper.updateTrack(rowId, editTitle, editTrack);
                }
                fillData();
                break;
        }
    }
}
